﻿@using viBook.Controllers
@using viBook.Models
@using viBook.Repositories
@{
    IUserRepository userRepository = new UserRepository();
    string avatarUrl = string.Empty;
    
    var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    if (SessionPersiter.User == null && cookie != null)
    {
        var ticket = FormsAuthentication.Decrypt(cookie.Value);
        string name = ticket.Name;
        
        SessionPersiter.User = userRepository.FindByEmail(name);
        
    }
    if (SessionPersiter.User != null)
    {
        avatarUrl = SessionPersiter.User.AvatarUrl;
    }
}
@if (SessionPersiter.User != null)
{
    <li id="t-profile"><a href="@Url.Action("Index", "Account", new { id = SessionPersiter.User.Id })" class="url" rel="contact">
        <img alt="" class="photo fn" height="14" src="@avatarUrl" width="14" id="avatar_icon"/>
        You</a>
        <ul class="tabs">
        @if (SessionPersiter.User.IsAdmin)
        {
            <li><a href="@Url.Action("Index", "Admins")">Admin page</a></li>
        }
            <li><a href="@Url.Action("Details", "Account")">Your profile</a></li>
            <li><a href="@Url.Action("Index", "Account", new { id = SessionPersiter.User.Id })">Your account</a></li>
            <li><a href="@Url.Action("LogOff", "Account")">Sign out</a></li>
        </ul>
    </li>
}
else
{
    <li id="t-signup">
        @Html.ActionLink("Register", "Register", "Account")</li>
    <li id="t-signin">
        @Html.ActionLink("Log on", "Logon", "Account")</li>
}
<script type="text/javascript">
    $("span#user_tab").mouseenter(function () {
        $("ul#user_profile").css("display", "block");
    });
    $("span#user_tab").mouseleave(function () {
        $("ul#user_profile").css("display", "none");
    });
</script>
